<?

class Page extends Container
{
	
    public function __construct($id = null, $version = null, $parent_id = null, $left_val = null, $right_val = null, $stage_state = null)
    {
    	parent::__construct($id, $version, $parent_id, $left_val, $right_val, $stage_state);
		
		$db_fields = array(
			'page_title' => new Char_field(
				array(
					'field_name' => 'page_title',
					'help_text' => 'Title of the page.'
				)
			),
			'menu_title' => new Char_field(
				array(
					'field_name' => 'menu_title',
					'help_text' => 'Alternate title (often shorter) for uses such as in a menu.'
				)
			),
			'page_address' => new Url_field(
				array(
					'field_name' => 'page_address',
					'help_text' => 'URL of the page.'
				)
			),
			'meta_title' => new Char_field(
				array(
					'field_name' => 'meta_title',
					'help_text' => 'The actual content of the &#60;title&#62; tag.'
				)
			),
			'meta_keywords' => new Text_field(
				array(
					'field_name' => 'meta_keywords',
					'help_text' => 'Meta data used to help with search engine placement.'
				)
			),
			'meta_description' => new Text_field(
				array(
					'field_name' => 'meta_description',
					'help_text' => 'Meta data used to help with search engine placement.'
				)
			),
			'page_content' => new Html_field(
				array(
					'field_name' => 'page_content',
					'help_text' => 'The content of the page.'
				)
			)
		);
		
		$this->initialize_db_fields($db_fields);
		
		// After initialization, populate the field
		if($id !== null && $version !== null)
		{
			// populate the fields from the DB
			$this->populate($id, $version);
		}
		
		$this->add_field_to_tab('Content', 'page_title', 'before', 'edit_comments');
		$this->add_field_to_tab('Content', 'menu_title', 'after', 'page_title');
		$this->add_field_to_tab('Content', 'page_content', 'after', 'menu_title');
		
		$this->add_field_to_tab('Meta Data', 'page_address');
		$this->add_field_to_tab('Meta Data', 'meta_title');
		$this->add_field_to_tab('Meta Data', 'meta_keywords');
		$this->add_field_to_tab('Meta Data', 'meta_description');
		
		$this->add_tab('Versions');
		//print_r($this->_versions);
    } // end __construct
    
    protected function on_after_create()
	{
		// Add the home page and other default content
		$query = $this->db->query("
			INSERT INTO `" . $this->db->dbprefix . $this->db_table . "` (
				`id`,
				`object_id`,
				`object_version` ,
				`edit_comments` ,
				`page_title` ,
				`menu_title` ,
				`page_address` ,
				`meta_title` ,
				`page_content`
			)
			VALUES
			(
				'1',
				'1',
				'1',
				'Default content.',
				'Site Root',
				'Site Root',
				null,
				'Site Root',
				'<p>This is not a page. This is the object under which all site pages are stored.</p>'
			),
			(
				'2',
				'2',
				'1',
				'Default content.',
				'Home Page',
				'Home Page',
				'home',
				'Home Page',
				'<p>You have successfully installed Phlare! Edit this page to get started.</p>'
			),
			(
				'3',
				'3',
				'1',
				'Default content.',
				'Error Pages',
				'Error Pages',
				'error-pages',
				'Error Pages',
				'<p>Error pages.</p>'
			),
			(
				'4',
				'4',
				'1',
				'Default content.',
				'Error 404',
				'Error 404',
				'error-404',
				'Error 404',
				'<p>Error 404. Page not found.</p>'
			)
		");
	} // end on_after_create
    
} // end Page